package com.tbsoft.ehr.dao.resume;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.tbsoft.ehr.dao.BaseDaoImpl;
import com.tbsoft.ehr.model.ProjectExperience;
import com.tbsoft.ehr.model.Resume;

/**
 * @desc 简历Dao实现
 * @author vastdream
 * 
 */
public class ResumeDaoImpl extends BaseDaoImpl<Resume> implements ResumeDao {
	private Log log = LogFactory.getLog(getClass());

	@SuppressWarnings("unchecked")
	@Override
	public List<Resume> getResumeList(final Resume resume,
			final int startIndex, final int count) {

		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Resume.class);
				// (Criteria) DetachedCriteria.forClass(Resume.class);
				criteria.add(Restrictions.ne("status", "D"));
				if (resume.getName() != null && !resume.getName().isEmpty()) {
					criteria.add(Restrictions.like("name", resume.getName(),MatchMode.ANYWHERE));
				}
				if (resume.getPost() != null && !resume.getPost().isEmpty()) {
					criteria.add(Restrictions.eq("post", resume.getPost()));
				}
				if (resume.getEducationLevel() != null && !resume.getEducationLevel().isEmpty()) {
					criteria.add(Restrictions.eq("educationLevel", resume.getEducationLevel()));
				}
				if(count != 0){
					criteria.setFirstResult(startIndex);
					criteria.setMaxResults(count);
				}
				return criteria.list();
			}
		});
		return list;

	}
	@Override
	public List<Resume> getResumeList(Resume resume) {
		return getResumeList(resume, 0, 0);
	}
	@Override
	public int getResumeCount(final Resume resume) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria criteria = session.createCriteria(Resume.class);
				// (Criteria) DetachedCriteria.forClass(Resume.class);
				criteria.add(Restrictions.ne("status", "D"));
				if (resume.getName() != null && !resume.getName().isEmpty()) {
					criteria.add(Restrictions.like("name", resume.getName(),MatchMode.ANYWHERE));
				}
				if (resume.getPost() != null && !resume.getPost().isEmpty()) {
					criteria.add(Restrictions.eq("post", resume.getPost()));
				}
				if (resume.getEducationLevel() != null && !resume.getEducationLevel().isEmpty()) {
					criteria.add(Restrictions.eq("educationLevel", resume.getEducationLevel()));
				}
				return criteria.list();
			}
		});
		return list.size();
	}

	@Override
	public Resume getResumeById(String id) {
		Resume tmpResume = (Resume) getHibernateTemplate()
				.get(Resume.class, id);
		List<ProjectExperience> tmpList = new ArrayList<ProjectExperience>();
		for (ProjectExperience prj : tmpResume.getProjectExperienceList()) {
			if (!"D".equals(prj.getStatus())) {
				log.info("project name:" + prj.getProjectName());
				tmpList.add(prj);
			}
		}
		tmpResume.setProjectExperienceList(tmpList);

		return tmpResume;
	}

	@Override
	public Resume getResumeByUserId( final String userid) {		
		Resume tmpResume =(Resume) getHibernateTemplate().execute(new HibernateCallback() {		
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Query q= session.createQuery("from Resume r where r.userId = ? and r.status<>'D'");
				q.setString(0, userid);
				return q.list().size()>0?q.list().get(0):null;
			}
		});
		if(tmpResume!=null){
			List<ProjectExperience> tmpList = new ArrayList<ProjectExperience>();
			for (ProjectExperience prj : tmpResume.getProjectExperienceList()) {
				if (!"D".equals(prj.getStatus())) {
					tmpList.add(prj);
				}
			}
			tmpResume.setProjectExperienceList(tmpList);
		}
			
		return tmpResume;
	}
	@SuppressWarnings("unchecked")
	public List<Resume> getResumeByListId(Set<String> idList){
		DetachedCriteria criteria = DetachedCriteria.forClass(Resume.class);
		
		criteria.add(Restrictions.ne("status", "D"));
		criteria.add(Restrictions.in("id", idList));
		return (List<Resume>) getHibernateTemplate().findByCriteria(criteria);
	}
}
